/**
 * @author EasonChan
 *
 * Dec 23, 2010
 */
package com.musicgeek.biz.util;

import com.musicgeek.util.SecurityUtil;

public class UserSecurityUtil {
	// The default digest algorithm.
	private static final String DEFAULT_DIGEST_ALGORITHM = "SHA1";
	// The default radix for encryption data output sequence.
	private static int ENCRYPTION_RADIX = 16;
	// The default spliter for login token.
	public static final String LOGIN_TOKEN_SPLITER = "_";

	/**
	 * @param data
	 *            The password to be encrypt.
	 * @return The encrypted password
	 */
	public static String encryptPassword(String password) {
		return SecurityUtil.getDigest(password, DEFAULT_DIGEST_ALGORITHM,
				ENCRYPTION_RADIX);
	}

	/**
	 * @param prefix
	 *            Prefix string for generate login token prefix.
	 * @param suffix
	 *            Suffix string for generate login token suffix.
	 * @return The login Token.
	 */
	public static String generateLoginToken(String prefix, String suffix) {
		String tokenPrefix = generateLoginTokenPrefix(prefix);
		String tokenSuffix = generateLoginTokenSuffix(suffix);
		return compositeLoginToken(tokenPrefix, tokenSuffix);
	}

	private static String compositeLoginToken(String tokenPrefix,
			String tokenSuffix) {
		return tokenPrefix + LOGIN_TOKEN_SPLITER + tokenSuffix;
	}

	private static String generateLoginTokenPrefix(String data) {
		return SecurityUtil.encodeBase64(data);
	}

	private static String generateLoginTokenSuffix(String suffix) {
		return SecurityUtil.getDigest(suffix, DEFAULT_DIGEST_ALGORITHM,
				ENCRYPTION_RADIX);
	}
}
